package com.maple.common.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.maple.common.ext.dpToPx

/**
 * Tip:
 * Create by SeVen on 2023/11/27 11:08
 */
class CircleBarView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    /**
     * 圆弧进度条宽度
     */
    private val barWidth = 0f

    /**
     * 锚点小圆半径
     */
    private val anchorRadius = 0f

    /**
     * 锚点小圆背景半径
     */
    private var anchorBgRadius = 0f

    /**
     * 背景圆弧的起始角度
     */
    private var startAngle = 0f

    /**
     * 背景圆弧扫过的角度
     */
    private var sweepAngle = 0f


    /**
     * 弦长
     */
    private var circleWidth = dpToPx(200f)

    /**
     * 弦高
     */
    private var circleHeight = dpToPx(100f)


    private var xDiff = 0f

    /**
     * 绘制圆弧的矩形区域
     */
    private val mRectF: RectF = RectF()

    init {

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //进度条的总轮廓
        val paint = Paint()
        paint.style = Paint.Style.STROKE
        paint.pathEffect = DashPathEffect(floatArrayOf(1f, 10f), 0f)
        paint.strokeWidth = dpToPx(10f)
        paint.isAntiAlias = true
        paint.color = Color.WHITE
        canvas.drawArc(mRectF, startAngle, sweepAngle, false, paint)
        val outCircleRadius = mRectF.width() / 2 //带宽度的进度条的中线半径


//        //进度条
//        canvas?.drawArc(mRectF!!, startAngle.toFloat(), progressSweepAngle, false, progressPaint)
//        val anchorAngle: Float = progressSweepAngle + (startAngle - 180)
//
//        //进度条前面的小圆
//        val cosValue = cos(Math.toRadians(anchorAngle.toDouble()))
//        val sinValue = sin(Math.toRadians(anchorAngle.toDouble()))
//        val cx: Float = (outCircleRadius - cosValue * outCircleRadius + anchorBgRadius).toFloat()
//        val cy: Float = (outCircleRadius - sinValue * outCircleRadius + anchorBgRadius).toFloat()
//        canvas?.drawCircle(cx - xDiff, cy, anchorRadius, anchorPaint) //锚点小圆
//        canvas?.drawCircle(cx - xDiff, cy, anchorBgRadius, anchorBgPaint) //锚点背景大圆

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //int height=measureSize(defaultSize,heightMeasureSpec);
        //int width=measureSize(defaultSize,widthMeasureSpec);
        //startAngle是计算出来的，因为弦的这种，没法通过观察直接得出圆弧的起始角度
        //通过数学计算得知x
        //x是圆心（圆弧所在的圆的圆心）到弦（也就是圆弧的底部）的垂直距离
        //int height=measureSize(defaultSize,heightMeasureSpec);
        //int width=measureSize(defaultSize,widthMeasureSpec);
        //startAngle是计算出来的，因为弦的这种，没法通过观察直接得出圆弧的起始角度
        //通过数学计算得知x
        //x是圆心（圆弧所在的圆的圆心）到弦（也就是圆弧的底部）的垂直距离
        val x = (circleWidth * circleWidth - 4 * circleHeight * circleHeight) / (8 * circleHeight)
        //画圆弧用到的圆的半径
        //画圆弧用到的圆的半径
        val r = x + circleHeight
        //int r=(int)(circleWidth/2/Math.cos(Math.toRadians(startAngle-180)));
        //弦长/2/半径=cos(startAngle-180)
        //(startAngle-180)
        //int r=(int)(circleWidth/2/Math.cos(Math.toRadians(startAngle-180)));
        //弦长/2/半径=cos(startAngle-180)
        //(startAngle-180)
        val asin = Math.asin((circleWidth / 2 / r).toDouble())
        //Log.i("circle_bar","asin="+asin);
        //Log.i("circle_bar","asin="+asin);
        val a = Math.toDegrees(asin).toInt()
        //Log.i("circle_bar","a="+a);
        //Log.i("circle_bar","a="+a);
        startAngle = (180 + (90 - a)).toFloat()
        sweepAngle = (2 * a).toFloat()
        //int min=Math.max(width,height);//获取View最短边的长度
        //int min=Math.max(width,height);//获取View最短边的长度
        var width = (Math.sqrt(
            Math.pow((r + anchorBgRadius).toDouble(), 2.0) - Math.pow(
                x.toDouble(),
                2.0
            )
        ) * 2 + 0.5f + anchorBgRadius).toInt() //圆弧的宽度

        width = (2 * (circleWidth / 2 + anchorBgRadius) + 0.5f).toInt()
        val height = (circleHeight + anchorBgRadius + 0.5f + anchorBgRadius).toInt()
        //height=width;
        //height=width;
        xDiff = r - circleWidth / 2 //画布是从最左边开始画圆且显示的，不偏移的话，显示的效果不对。

        Log.i("circle_bar", "xDiff=$xDiff")
        setMeasuredDimension(width, height)
        if (Math.min(width, height) >= barWidth * 2) { //这里简单限制了圆弧的最大宽度
            //1.mRectF决定着圆画在哪个框框里面
            //2.mRectF在当前View中，但不是完全占据当前View
            //3.弧度的粗细画的时候是均分到圆半径线的两侧的
            //4.之所以矩形设置的四个点有偏移，是因为有弧度宽度。
            //因为锚点也有宽度，点再次做调整.
            //mRectF.set(barWidth/2,barWidth/2,min-barWidth/2,min-barWidth/2);
            val diffByAnchor = anchorBgRadius - barWidth / 2 //受锚点影响需要修正的偏移
            //是整个圆的矩形，不是弧形区域所在的矩形
            mRectF[barWidth / 2 + diffByAnchor - xDiff + 100, barWidth / 2 + diffByAnchor, barWidth / 2 + diffByAnchor + 2 * r - xDiff - 100] =
                barWidth / 2 + diffByAnchor + 2 * r
        }
    }
}